package org.pps.gl.client.vue;

import org.pps.gl.client.InscriptionService;
import org.pps.gl.client.InscriptionServiceAsync;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsDate;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * 2ème onglet de la page d'accueil pour s'inscrire
 * 
 */
public class ACL_Inscription extends Composite {

	// vérification pour mot de passe et date
	public static final String REGEX_EMAIL = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";

	private final TextBox textBoxEmail, textBoxNom, textBoxPrenom;
	private final Label lblErrEmail, lblErrNom, lblErrPrenom, lblErrPassword;
	private final Label lblErrDateNaissance, lblErrSexe, lblResultInscription;
	private final ListBox comboBoxSexe;
	private final PasswordTextBox textBoxPassword, textBoxPassword_2;
	private HorizontalPanel horizontalPanel;
	private ListBox listBox_D, listBox_M, listBox_Y;

	public ACL_Inscription() {

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel
		.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		initWidget(verticalPanel);
		verticalPanel.setWidth("450px");

		// affichage sous forme de tableau
		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		verticalPanel.setCellHorizontalAlignment(flexTable,
				HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(flexTable,
				HasVerticalAlignment.ALIGN_MIDDLE);

		Label lblEmail = new Label("Email");
		flexTable.setWidget(0, 0, lblEmail);
		flexTable.getCellFormatter().setHeight(0, 0, "30px");
		flexTable.getCellFormatter().setWidth(0, 0, "200px");
		lblEmail.setSize("200px", "30px");

		textBoxEmail = new TextBox();
		flexTable.setWidget(0, 1, textBoxEmail);
		flexTable.getCellFormatter().setHeight(0, 1, "35px");
		flexTable.getCellFormatter().setWidth(0, 1, "200px");
		textBoxEmail.setWidth("200px");

		lblErrEmail = new Label("");
		lblErrEmail.setStyleName("gwt-error-Label");
		lblErrEmail.setVisible(false);
		flexTable.setWidget(0, 2, lblErrEmail);
		lblErrEmail.setWidth("");

		Label lblNom = new Label("Nom");
		flexTable.setWidget(1, 0, lblNom);
		flexTable.getCellFormatter().setHeight(1, 0, "30px");
		flexTable.getCellFormatter().setWidth(1, 0, "200px");
		lblNom.setSize("200px", "20px");

		textBoxNom = new TextBox();
		flexTable.setWidget(1, 1, textBoxNom);
		flexTable.getCellFormatter().setHeight(1, 1, "35px");
		flexTable.getCellFormatter().setWidth(1, 1, "200px");
		textBoxNom.setWidth("200px");

		lblErrNom = new Label("");
		lblErrNom.setStyleName("gwt-error-Label");
		flexTable.setWidget(1, 2, lblErrNom);

		Label lblPrenom = new Label("Prénom");
		flexTable.setWidget(2, 0, lblPrenom);
		flexTable.getCellFormatter().setHeight(2, 0, "30px");
		flexTable.getCellFormatter().setWidth(2, 0, "200px");
		lblPrenom.setSize("200", "20px");

		textBoxPrenom = new TextBox();
		flexTable.setWidget(2, 1, textBoxPrenom);
		textBoxPrenom.setSize("200px", "");
		flexTable.getCellFormatter().setHeight(2, 1, "35px");
		flexTable.getCellFormatter().setWidth(2, 1, "200px");

		lblErrPrenom = new Label("");
		lblErrPrenom.setStyleName("gwt-error-Label");
		flexTable.setWidget(2, 2, lblErrPrenom);

		Label lblNewLabel = new Label("Mot de passe");
		flexTable.setWidget(3, 0, lblNewLabel);
		flexTable.getCellFormatter().setHeight(3, 0, "30px");
		flexTable.getCellFormatter().setWidth(3, 0, "200px");

		textBoxPassword = new PasswordTextBox();
		flexTable.setWidget(3, 1, textBoxPassword);
		textBoxPassword.setWidth("200px");
		flexTable.getCellFormatter().setHeight(3, 1, "35px");
		flexTable.getCellFormatter().setWidth(3, 1, "200px");

		lblErrPassword = new Label("");
		lblErrPassword.setStyleName("gwt-error-Label");
		flexTable.setWidget(3, 2, lblErrPassword);

		Label lblNewLabel_2 = new Label("Retaper le mot de passe");
		flexTable.setWidget(4, 0, lblNewLabel_2);

		textBoxPassword_2 = new PasswordTextBox();
		flexTable.setWidget(4, 1, textBoxPassword_2);
		flexTable.getCellFormatter().setHeight(4, 1, "35px");
		flexTable.getCellFormatter().setWidth(4, 1, "200px");
		textBoxPassword_2.setWidth("200px");

		Label lblErrPassword_retype = new Label("");
		lblErrPassword_retype.setStyleName("gwt-error-Label");
		flexTable.setWidget(4, 2, lblErrPassword_retype);

		Label lblDateDeNaissance = new Label("Date de naissance (J/M/A)");
		flexTable.setWidget(5, 0, lblDateDeNaissance);
		flexTable.getCellFormatter().setHeight(5, 0, "30px");
		flexTable.getCellFormatter().setWidth(5, 0, "200px");

		horizontalPanel = new HorizontalPanel();
		flexTable.setWidget(5, 1, horizontalPanel);
		horizontalPanel.setSize("100%", "100%");

		listBox_D = new ListBox();
		listBox_D.addItem("");
		for (Integer i = 1; i < 10; i++)
			listBox_D.addItem("0"+i.toString());
		for (Integer i = 10; i <= 31; i++)
			listBox_D.addItem(i.toString());
		horizontalPanel.add(listBox_D);

		listBox_M = new ListBox();
		listBox_M.addItem("");
		for (Integer i = 1; i < 10; i++)
			listBox_M.addItem("0"+i.toString());
		for (Integer i = 10; i <= 12; i++)
			listBox_M.addItem(i.toString());
		horizontalPanel.add(listBox_M);

		int y = JsDate.create().getFullYear();

		listBox_Y = new ListBox();
		listBox_Y.addItem("");
		for (Integer i = y-10; i >= y-100; i--)
			listBox_Y.addItem(i.toString());
		horizontalPanel.add(listBox_Y);

		lblErrDateNaissance = new Label("");
		lblErrDateNaissance.setStyleName("gwt-error-Label");
		flexTable.setWidget(5, 2, lblErrDateNaissance);

		Label lblNewLabel_1 = new Label("Genre");
		flexTable.setWidget(6, 0, lblNewLabel_1);
		flexTable.getCellFormatter().setHeight(6, 0, "30px");
		flexTable.getCellFormatter().setWidth(6, 0, "200px");

		comboBoxSexe = new ListBox();
		comboBoxSexe.addItem("");
		comboBoxSexe.addItem("M");
		comboBoxSexe.addItem("F");
		flexTable.setWidget(6, 1, comboBoxSexe);
		flexTable.getCellFormatter().setVerticalAlignment(1, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setVerticalAlignment(3, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		flexTable.getCellFormatter().setHorizontalAlignment(6, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_BOTTOM);

		lblErrSexe = new Label("");
		lblErrSexe.setStyleName("gwt-error-Label");
		flexTable.setWidget(6, 2, lblErrSexe);

		lblResultInscription = new Label("");
		lblResultInscription.setStyleName("gwt-Label-formOk");
		flexTable.setWidget(7, 1, lblResultInscription);
		lblResultInscription.setSize("210px", "33px");
		lblResultInscription.setVisible(false);

		Button btnInscription = new Button("Inscription");
		flexTable.setWidget(8, 1, btnInscription);

		// Lors du click sur inscription
		btnInscription.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				// Remise à zero de tous les champs d'information
				razChampsInfo();

				// Vérification des saisies de l'utilisateur
				boolean erreur = false;

				// Email
				if (textBoxEmail.getText().length() == 0) {
					lblErrEmail.setText("champ obligatoire");
					lblErrEmail.setVisible(true);
					erreur = true;
				} else if (!textBoxEmail.getText().matches(REGEX_EMAIL)) {
					// Si non format email
					lblErrEmail.setText("format incorrect");
					lblErrEmail.setVisible(true);
					erreur = true;
				}
				// Nom
				if (textBoxNom.getText().length() == 0) {
					lblErrNom.setText("champ obligatoire");
					lblErrNom.setVisible(true);
					erreur = true;
				}
				// else if () // Si format nok

				// Prénom
				if (textBoxPrenom.getText().length() == 0) {
					lblErrPrenom.setText("champ obligatoire");
					lblErrPrenom.setVisible(true);
					erreur = true;
				}
				// else if () // Si format nok

				// Champ de mot de passe identiques et de longueur suffisante
				if (!textBoxPassword.getText().equals(
						textBoxPassword_2.getText())) {
					System.out.println(textBoxPassword.getText() + " // "
							+ textBoxPassword_2.getText());
					lblErrPassword.setText("mot de passe différent");
					lblErrPassword.setVisible(true);
					erreur = true;
				} else if (textBoxPassword.getText().length() < 6) {
					lblErrPassword
							.setText("6 caractères minimum");
					lblErrPassword.setVisible(true);
					erreur = true;
				}

				// Date naissance
				String d = "null";
				if (listBox_D.getSelectedIndex() == 0
						|| listBox_M.getSelectedIndex() == 0
						|| listBox_Y.getSelectedIndex() == 0) {
					lblErrDateNaissance.setText("champ obligatoire");
					lblErrDateNaissance.setVisible(true);
					erreur = true;
				} else
					d = listBox_D.getItemText(listBox_D.getSelectedIndex())
							+ "/"
							+ listBox_M.getItemText(listBox_M
									.getSelectedIndex())
							+ "/"
							+ listBox_Y.getItemText(listBox_Y
									.getSelectedIndex());

				// Combo box de choix du sexe
				if (comboBoxSexe.getSelectedIndex() == 0) {
					lblErrSexe.setText("champ obligatoire");
					lblErrSexe.setVisible(true);
					erreur = true;
				}
				
				// S'il n'y a pas d'erreur nous pouvons insérer le nouvel
				// utilisateur
				if (!erreur) {
					InscriptionServiceAsync inscriptionService = GWT
							.create(InscriptionService.class);
					inscriptionService.InscrireUtilisateur(textBoxEmail
							.getText(), textBoxNom.getText(), textBoxPrenom
							.getText(), textBoxPassword.getText(), d,
							comboBoxSexe.getValue(comboBoxSexe
									.getSelectedIndex()),
							new AsyncCallback<Integer>() {
								public void onFailure(Throwable error) {
									// Window.alert("No :(");
								}

								public void onSuccess(Integer result) {
									// Si l'utilisateur est bien inséré
									if (0 == result) {
										System.out
												.println("Utilisateur inséré : "
														+ result);
										lblResultInscription
												.setText("Utilisateur créé avec succès.");
										lblResultInscription.setVisible(true);
										razChampsTexte();
									} else if (1 == result) { // Si
																// l'utilisateur
																// existe deja
										System.out.println("Existe");
										lblErrEmail
												.setText("Email déjà existant.");
										lblErrEmail.setVisible(true);
									}
								}
							});
				}

			}
		});

	}

	/**
	 * Méthode de remise a zero des différents champs
	 */
	public void razChampsInfo() {
		lblErrDateNaissance.setVisible(false);
		lblErrEmail.setVisible(false);
		lblErrNom.setVisible(false);
		lblErrPassword.setVisible(false);
		lblErrPrenom.setVisible(false);
		lblErrSexe.setVisible(false);
		lblResultInscription.setVisible(false);
	}

	/**
	 * Méthode qui vide toutes les textbox.
	 */
	public void razChampsTexte() {
		textBoxEmail.setText("");
		textBoxNom.setText("");
		textBoxPassword.setText("");
		textBoxPassword_2.setText("");
		textBoxPrenom.setText("");
		listBox_D.setItemSelected(0, true);
		listBox_M.setItemSelected(0, true);
		listBox_Y.setItemSelected(0, true);
		comboBoxSexe.setItemSelected(0, true);
	}

}
